//
//  GSNewFeatureViewController.m
//  GameSky
//
//  Created by lanou on 16/1/22.
//  Copyright © 2016年 ZL. All rights reserved.
//

#import "GSNewFeatureViewController.h"
#import "ICSDrawerController.h"
#import "GSRootTabBarController.h"
#import "GSRootViewController.h"
#import "GSMainViewController.h"
#import "UIImage+animatedGIF.h"

#define k_Base_Tag  10000
#define k_Rotate_Rate 1

@interface GSNewFeatureViewController ()<UIScrollViewDelegate>

@end

@implementation GSNewFeatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:140.0/255 green:1 blue:1 alpha:1];
    
//    NSArray *imageArr = @[@"firstRunGuiderView_Image_1.png",@"firstRunGuiderView_Image_2.png",@"firstRunGuiderView_Image_3.png"];
    
    NSArray *imageArr = @[@"1",@"2",@"3"];
    
    NSArray *textImageArr = @[@"firstRunGuiderView_Title_1.png",@"firstRunGuiderView_Title_2.png",@"firstRunGuiderView_Title_3.png"];
    
    UIScrollView *mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    mainScrollView.pagingEnabled = YES;
    mainScrollView.bounces = YES;
    mainScrollView.contentSize = CGSizeMake(kScreenWidth*imageArr.count, kScreenHeight);
    mainScrollView.showsHorizontalScrollIndicator = NO;
    mainScrollView.delegate = self;
    [self.view addSubview:mainScrollView];
    
    //添加云彩图片
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 330, kScreenWidth, 170*kScreenWidth/1242.0)];
//    imageView.image = [UIImage imageNamed:@"firstRunGuiderView_Button_3.png"];
    [self.view addSubview:imageView];
    
    
    for (int i=0; i<imageArr.count; i++) {
        UIView *rotateView = [[UIView alloc]initWithFrame:CGRectMake(kScreenWidth*i, 0, kScreenWidth, kScreenHeight * 2)];
        [rotateView setTag:k_Base_Tag+i];
        [mainScrollView addSubview:rotateView];
        if (i!=0) {
            rotateView.alpha = 0;
        }

        
        UIImageView *bgImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        
        NSString *filePath = [[NSBundle mainBundle] pathForResource:imageArr[i] ofType:@"gif"];
        
        NSData *gif = [NSData dataWithContentsOfFile:filePath];
        
        bgImage.image = [UIImage animatedImageWithAnimatedGIFData:gif];
        
        [rotateView addSubview:bgImage];
        
        UIImageView *textImageView = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth*i, 50, kScreenWidth,kScreenHeight / 3)];
        [textImageView setTag:k_Base_Tag*2+i];
        textImageView.image = [UIImage imageNamed:textImageArr[i]];
        [mainScrollView addSubview:textImageView];
        
        //最后页面添加按钮
        if (i == imageArr.count-1) {
            
            UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
            
            button.frame =CGRectMake(0, 0, 150, 50);
            
            button.center = CGPointMake(kScreenWidth / 2, kScreenHeight / 2);
            
            [button setBackgroundImage:[UIImage imageNamed:@"firstRunGuiderView_Button_3"] forState:(UIControlStateNormal)];
            
            
            [button addTarget:self action:@selector(ClickToRemove) forControlEvents:UIControlEventTouchUpInside];
            
            [rotateView addSubview:button];
            
        }
    }
    
    UIView *firstView = [mainScrollView viewWithTag:k_Base_Tag];
    [mainScrollView bringSubviewToFront:firstView];
    
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    UIView * view1 = [scrollView viewWithTag:k_Base_Tag];
    UIView * view2 = [scrollView viewWithTag:k_Base_Tag+1];
    UIView * view3 = [scrollView viewWithTag:k_Base_Tag+2];
    UIView * view4 = [scrollView viewWithTag:k_Base_Tag+3];
    
    UIImageView * imageView1 = (UIImageView *)[scrollView viewWithTag:k_Base_Tag*2];
    UIImageView * imageView2 = (UIImageView *)[scrollView viewWithTag:k_Base_Tag*2+1];
    UIImageView * imageView3 = (UIImageView *)[scrollView viewWithTag:k_Base_Tag*2+2];
    UIImageView * imageView4 = (UIImageView *)[scrollView viewWithTag:k_Base_Tag*2+3];
    
    CGFloat xOffset = scrollView.contentOffset.x;
    
    //根据偏移量旋转
    CGFloat rotateAngle = -1 * 1.0/kScreenWidth * xOffset * M_PI_2 * k_Rotate_Rate;
    view1.layer.transform = CATransform3DMakeRotation(rotateAngle, 0, 0, 1);
    view2.layer.transform = CATransform3DMakeRotation(M_PI_2*1+rotateAngle, 0, 0, 1);
    view3.layer.transform = CATransform3DMakeRotation(M_PI_2*2+rotateAngle, 0, 0, 1);
    view4.layer.transform = CATransform3DMakeRotation(M_PI_2*3+rotateAngle, 0, 0, 1);
    
    //根据偏移量位移（保证中心点始终都在屏幕下方中间）
    view1.center = CGPointMake(0.5 * kScreenWidth+xOffset, kScreenHeight);
    view2.center = CGPointMake(0.5 * kScreenWidth+xOffset, kScreenHeight);
    view3.center = CGPointMake(0.5 * kScreenWidth+xOffset, kScreenHeight);
    view4.center = CGPointMake(0.5 * kScreenWidth+xOffset, kScreenHeight);
    
    //当前哪个视图放在最上面
    if (xOffset<kScreenWidth*0.5) {
        [scrollView bringSubviewToFront:view1];
        
    }else if (xOffset>=kScreenWidth*0.5 && xOffset < kScreenWidth*1.5){
        [scrollView bringSubviewToFront:view2];
        
        
    }else if (xOffset >=kScreenWidth*1.5 && xOffset < kScreenWidth*2.5){
        [scrollView bringSubviewToFront:view3];
        
    }else if (xOffset >=kScreenWidth*2.5)
    {
        [scrollView bringSubviewToFront:view4];
        
    }
    
    //调节其透明度
    CGFloat xoffset_More = xOffset*1.5>kScreenWidth?kScreenWidth:xOffset*1.5;
    if (xOffset < kScreenWidth) {
        view1.alpha = (kScreenWidth - xoffset_More)/kScreenWidth;
        imageView1.alpha = (kScreenWidth - xOffset)/kScreenWidth;;
        
    }
    if (xOffset <= kScreenWidth) {
        view2.alpha = xoffset_More / kScreenWidth;
        imageView2.alpha = xOffset / kScreenWidth;
    }
    if (xOffset >kScreenWidth && xOffset <= kScreenWidth*2) {
        view2.alpha = (kScreenWidth*2 - xOffset)/kScreenWidth;
        view3.alpha = (xOffset - kScreenWidth)/ kScreenWidth;
        
        imageView2.alpha = (kScreenWidth*2 - xOffset)/kScreenWidth;
        imageView3.alpha = (xOffset - kScreenWidth)/ kScreenWidth;
    }
    if (xOffset >kScreenWidth*2 ) {
        view3.alpha = (kScreenWidth*3 - xOffset)/kScreenWidth;
        view4.alpha = (xOffset - kScreenWidth*2)/ kScreenWidth;
        
        imageView3.alpha = (kScreenWidth*3 - xOffset)/kScreenWidth;
        imageView4.alpha = (xOffset - kScreenWidth*2)/ kScreenWidth;
    }
    
    //调节背景色
    if (xOffset <kScreenWidth && xOffset>0) {
        self.view.backgroundColor = [UIColor colorWithRed:(140-40.0/kScreenWidth*xOffset)/255.0 green:(255-25.0/kScreenWidth*xOffset)/255.0 blue:(255-100.0/kScreenWidth*xOffset)/255.0 alpha:1];
        
    }else if (xOffset>=kScreenWidth &&xOffset<kScreenWidth*2){
        
        self.view.backgroundColor = [UIColor colorWithRed:(100+30.0/kScreenWidth*(xOffset-kScreenWidth))/255.0 green:(230-40.0/kScreenWidth*(xOffset-kScreenWidth))/255.0 blue:(155-5.0/320*(xOffset-kScreenWidth))/255.0 alpha:1];
        
    }else if (xOffset>=kScreenWidth*2 &&xOffset<kScreenWidth*3){
        
        self.view.backgroundColor = [UIColor colorWithRed:(130-50.0/kScreenWidth*(xOffset-kScreenWidth*2))/255.0 green:(190-40.0/kScreenWidth*(xOffset-kScreenWidth*2))/255.0 blue:(150+50.0/kScreenWidth*(xOffset-kScreenWidth*2))/255.0 alpha:1];
        
    }else if (xOffset>=kScreenWidth*3 &&xOffset<kScreenWidth*4){
        
        self.view.backgroundColor = [UIColor colorWithRed:(80-10.0/kScreenWidth*(xOffset-kScreenWidth*3))/255.0 green:(150-25.0/kScreenWidth*(xOffset-kScreenWidth*3))/255.0 blue:(200-90.0/kScreenWidth*(xOffset-kScreenWidth*3))/255.0 alpha:1];
    }
    
}

-(void)ClickToRemove
{
    [self.view removeFromSuperview];
    
    GSRootTabBarController *tab = [[GSRootTabBarController alloc]init];
    
    GSRootViewController *root = [[GSRootViewController alloc]init];
    
    ICSDrawerController *ics = [[ICSDrawerController alloc]initWithLeftViewController:root centerViewController:tab];

    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    // 应为展示完新特性之后 显示界面 这个过程是不可逆的,没有必要还能返回来看新特性 所以用这种方法
    window.rootViewController = ics;
    
    
    
}
-(BOOL)shouldAutorotate
{
    return YES;
}

-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
